package com.cy.sweepgradientprogress;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

/**
 * 圆形颜色渐变的进度条
 * @author soyoungboy
 *
 */
public class SweepGradientCircleProgressBar extends View {
	private Paint railSidePaint;
	private Paint fillArcPaint;
	private Paint begainCircelPaint;
	private Paint indicatorCircelPaint;
	//渐变数组
	private int[] arcColors = new int[] {Color.RED,Color.TRANSPARENT};
	private RectF oval ;

	//进度条宽度
	private int fillArcWidth = 30;
	//初始化进度
	private float progress = 0;
	//设置进度最大值
	private int max = 100;
	//轨道两侧圆环的宽度
	private int railSideWidth = 20;

	public SweepGradientCircleProgressBar(Context context ,AttributeSet attrs) {
		super(context,attrs);
		initPaint();
		oval = new RectF();
	}

	//初始化画笔操作
	private void initPaint() {
		//轨道两侧的圆环
		railSidePaint = new Paint();
		railSidePaint.setAntiAlias(true);
		railSidePaint.setStyle(Paint.Style.STROKE);
		railSidePaint.setDither(true);
		railSidePaint.setStrokeJoin(Paint.Join.ROUND);
		railSidePaint.setStrokeWidth(20);
		railSidePaint.setColor(Color.GRAY);

		//渐变色进度圆环
		fillArcPaint = new Paint();
		fillArcPaint.setAntiAlias(true);
		fillArcPaint.setStyle(Paint.Style.STROKE);
		fillArcPaint.setDither(true);
		fillArcPaint.setStrokeJoin(Paint.Join.ROUND);
		// 设置线的类型,线头是圆的
		fillArcPaint.setStrokeCap(Paint.Cap.ROUND);

		//开始接头处的圆点，通过遮挡解决SweepGradient影响的接头颜色过渡问题
		begainCircelPaint = new Paint();
		begainCircelPaint.setAntiAlias(true);
		begainCircelPaint.setColor(arcColors[0]);

		//进度指示圆点，暂默认为白色
		indicatorCircelPaint = new Paint();
		indicatorCircelPaint.setAntiAlias(true);
		indicatorCircelPaint.setColor(0xffffffff);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.save();
		canvas.rotate(90,width / 2,height/2);

		drawcircle(canvas);
		drawBeginLittleCircle(canvas);
		drawIndecatorCircle(canvas);

		canvas.restore();
	}

	//画圆遮挡住SweepGradient接头处的部分
	private void drawBeginLittleCircle(Canvas canvas) {
		canvas.drawCircle(width- fillArcWidth-railSideWidth -1f,height/2, fillArcWidth /2+0.5f,begainCircelPaint);
	}

	private void drawIndecatorCircle(Canvas canvas) {
		float r=width/2- fillArcWidth-railSideWidth;
		float radian = (float) Math.toRadians((progress /max ) * 360);
		float x = (float) (width/2 + Math.cos(radian)*r);
		float y = (float) (height/2 + Math.sin(radian)*r);

		canvas.drawCircle(x, y, fillArcWidth /3,indicatorCircelPaint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		height = getMeasuredWidth();
		width = getMeasuredWidth();
	}

	int height;
	int width;

	private void drawcircle(Canvas canvas) {
		//半径 = 宽/2-圆环的宽度
		int fillArcRadius = width/2- fillArcWidth-railSideWidth;
		int cx = width/2;
		int cy = height/2;
		//绘制大圆
		canvas.drawCircle(width / 2, height / 2, fillArcRadius + fillArcWidth / 2+ railSideWidth/2 + 0.5f,
				railSidePaint);
		//绘制小圆
		canvas.drawCircle(width / 2, height / 2, fillArcRadius - fillArcWidth / 2- railSideWidth/2 - 0.5f,
				railSidePaint);

		// 环形颜色填充
		SweepGradient sweepGradient =
				new SweepGradient(width / 2, height / 2, arcColors, null);
		fillArcPaint.setShader(sweepGradient);


		//设置圆弧的宽度
		fillArcPaint.setStrokeWidth(fillArcWidth +1);
		// 确定圆弧的绘制位置，也就是里面圆弧坐标和外面圆弧坐标  
		oval.set(width / 2 - fillArcRadius, height / 2 - fillArcRadius, width
				/ 2 + fillArcRadius, height / 2 + fillArcRadius);
		// 画圆弧，第二个参数为：起始角度，第三个为跨的角度，第四个为true的时候是实心，false的时候为空心
		canvas.drawArc(oval,
				0,
				(progress /max ) * 360,
				false,
				fillArcPaint);
	}

	public float getProgress() {
		return progress;
	}

	public void setProgress(float progress) {
		this.progress = progress;
		this.invalidate();
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public void setArcColors(int[] arcColors) {
		this.arcColors = arcColors;
		begainCircelPaint.setColor(arcColors[0]);
	}

	public void startAnim(float progress){
		ObjectAnimator anim = ObjectAnimator.ofFloat(this, "progress", 0,progress);
		anim.setInterpolator(new AccelerateDecelerateInterpolator());
		anim.setDuration(1500);
		anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator valueAnimator) {
//				Log.i("tag",valueAnimator.getAnimatedValue("percent")+"");
//				float value= (float) valueAnimator.getAnimatedValue("percent");
//				tvPercent.setText(Math.round(value+0.5)+"");
			}
		});
		anim.start();

	}
}
